home *** CD-ROM | disk | FTP | other *** search
/ Internet.Works 39 / Issue 39.iso / pc / PCSoftware / EditML Pro 1.0 / EditMLProv1.0Setup.exe / Main / stock-sorter.xsl < prev    next >
Encoding:
Extensible Markup Language  |  2000-10-09  |  5.4 KB  |  136 lines

  1. <?xml version="1.0"?>
  2.  
  3. <!-- Note that this stylesheet relies on the XMLDocument and XSLDocument properties and thus
  4.      only works when browsing XML -->
  5.      
  6. <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
  7.   <xsl:template match="/">
  8.     <HTML>
  9.       <HEAD>
  10.         <STYLE>
  11.           BODY {margin:0}
  12.           .bg {font:8pt Verdana; background-color:purple; color:white}
  13.           H1 {font:bold 14pt Verdana; width:100%; margin-top:1em}
  14.           .row {font:8pt Verdana; border-bottom:1px solid #CC88CC}
  15.           .header {font:bold 9pt Verdana; cursor:hand; padding:2px; border:2px outset gray}
  16.           .up {background-color:#DDFFDD;}
  17.           .down {background-color:#FFDDDD;}
  18.         </STYLE>
  19.       </HEAD>
  20.       
  21.       <SCRIPT><xsl:comment><![CDATA[
  22.         function sort(field)
  23.         {
  24.           sortField.value = field;
  25.           <!-- set cursor to watch here? -->
  26.           listing.innerHTML = source.documentElement.transformNode(stylesheet);
  27.         }
  28.       ]]></xsl:comment></SCRIPT>
  29.  
  30.       <SCRIPT for="window" event="onload"><xsl:comment><![CDATA[
  31.         stylesheet = document.XSLDocument;
  32.         source = document.XMLDocument;
  33.         sortField = document.XSLDocument.selectSingleNode("//@order-by");
  34.       ]]></xsl:comment></SCRIPT>
  35.       
  36.       <BODY>
  37.         <TABLE width="100%" cellspacing="0">
  38.           <TR>
  39.             <TD class="bg"/>
  40.             <TD class="bg">
  41.               <H1><xsl:value-of select="portfolio/description"/>
  42.                 for <xsl:apply-templates select="portfolio/date"/></H1>
  43.               <DIV>Average change: <B><xsl:eval>averageChange(this)</xsl:eval></B></DIV>
  44.               <DIV>Total volume: <B><xsl:eval>totalVolume(this)</xsl:eval></B></DIV>
  45.             </TD>
  46.           </TR>
  47.           <TR>
  48.             <TD class="bg" width="120" valign="top">
  49.               <P>Click on the column headers to sort by that field.</P>
  50.               <P>Demonstration of custom formatting of data typed values and
  51.               local reapplication of the stylesheet.</P>
  52.               <P>Stocks losing more than 5% indicated in red.  Stocks gaining
  53.               value indicated in green.</P>
  54.             </TD>
  55.             <TD class="bg" valign="top">
  56.               <DIV id="listing"><xsl:apply-templates match="portfolio"/></DIV>
  57.             </TD>
  58.           </TR>
  59.         </TABLE>    
  60.       </BODY>
  61.     </HTML>
  62.   </xsl:template>
  63.   
  64.   <xsl:template match="portfolio">
  65.     <TABLE STYLE="background-color:white">
  66.       <THEAD>
  67.         <TD width="200"><DIV class="header" onClick="sort('name')">Company</DIV></TD>
  68.         <TD width="80"><DIV class="header" onClick="sort('symbol')">Symbol</DIV></TD>
  69.         <TD width="80"><DIV class="header" onClick="sort('price')">Price</DIV></TD>
  70.         <TD width="80"><DIV class="header" onClick="sort('change')">Change</DIV></TD>
  71.         <TD width="80"><DIV class="header" onClick="sort('percent')">%Change</DIV></TD>
  72.         <TD width="80"><DIV class="header" onClick="sort('volume')">Volume</DIV></TD>
  73.       </THEAD>
  74.       <xsl:for-each select="stock" order-by="symbol">
  75.         <TR>
  76.           <xsl:for-each select="change">
  77.             <xsl:if expr="this.nodeTypedValue > 0">
  78.               <xsl:attribute name="class">up</xsl:attribute>
  79.             </xsl:if>
  80.           </xsl:for-each>
  81.           <xsl:for-each select="percent">
  82.             <xsl:if expr="this.nodeTypedValue < -5">
  83.               <xsl:attribute name="class">down</xsl:attribute>
  84.             </xsl:if>
  85.           </xsl:for-each>
  86.           <TD><DIV class="row"><xsl:value-of select="name"/></DIV></TD>
  87.           <TD><DIV class="row"><xsl:value-of select="symbol"/></DIV></TD>
  88.           <TD><DIV class="row" STYLE="text-align:right"><xsl:apply-templates select="price"/></DIV></TD>
  89.           <TD><DIV class="row" STYLE="text-align:right"><xsl:apply-templates select="change"/></DIV></TD>
  90.           <TD><DIV class="row" STYLE="text-align:right"><xsl:apply-templates select="percent"/></DIV></TD>
  91.           <TD><DIV class="row" STYLE="text-align:right"><xsl:apply-templates select="volume"/></DIV></TD>
  92.         </TR>
  93.       </xsl:for-each>
  94.     </TABLE>
  95.   </xsl:template>
  96.  
  97.   <xsl:template match="date">
  98.     <xsl:eval>formatDate(this.nodeTypedValue, "MMMM dd',' yyyy")</xsl:eval>
  99.               at <xsl:eval>formatTime(this.nodeTypedValue, "hh:mm tt")</xsl:eval>
  100.   </xsl:template>
  101.   
  102.   <xsl:template match="price | change">
  103.     <xsl:eval>formatNumber(this.nodeTypedValue, "$0.00")</xsl:eval>
  104.   </xsl:template>
  105.   
  106.   <xsl:template match="percent">
  107.     <xsl:if expr="this.nodeTypedValue > 0">+</xsl:if>
  108.     <xsl:eval>formatNumber(this.nodeTypedValue, "0.0")</xsl:eval>%
  109.   </xsl:template>
  110.   
  111.   <xsl:template match="volume">
  112.     <xsl:eval>formatNumber(this.nodeTypedValue * 1000000, "#,###,###")</xsl:eval>
  113.   </xsl:template>
  114.   
  115.   <xsl:script><![CDATA[
  116.     function totalVolume(node)
  117.     {
  118.       total = 0;
  119.       volumes = node.selectNodes("/portfolio/stock/volume");
  120.       for (v = volumes.nextNode(); v; v = volumes.nextNode())
  121.         total += v.nodeTypedValue;
  122.       return formatNumber(total, "#") + " million shares";
  123.     }
  124.     
  125.     function averageChange(node)
  126.     {
  127.       total = 0;
  128.       percents = node.selectNodes("/portfolio/stock/percent");
  129.       count = percents.length;
  130.       for (p = percents.nextNode(); p; p = percents.nextNode())
  131.         total += p.nodeTypedValue;
  132.       return formatNumber(total/count, "#.0") + "%";
  133.     }
  134.   ]]></xsl:script>
  135. </xsl:stylesheet>
  136.